package com.yt.user.service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.common.utils.Md5Utils;
import com.yt.user.common.config.YmlConfig;
import com.yt.user.mapper.CustomerInfoMapper;
import com.yt.user.models.bo.CustomerInfoSaveBO;
import com.yt.user.models.dto.CustomerInfoDTO;
import com.yt.user.models.dto.LoginInfoDTO;
import com.yt.user.models.service.ICustomerInfoService;
import com.yt.user.models.service.ILoginInfoService;
import com.yt.user.models.vo.CustomerInfoVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 客户信息控制器
 *
 * @author zhou
 * @Date 2025-09-08 15:43:37
 */
@DubboService
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfoDTO> implements ICustomerInfoService {

    @Resource
    private YmlConfig ymlConfig;
    @Resource
    private ILoginInfoService loginInfoService;

    @Override
    public boolean add(CustomerInfoSaveBO saveBO) {
        CustomerInfoDTO dto = new CustomerInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public List<CustomerInfoVO> queryCustomerList() {
        List<CustomerInfoVO> voList = baseMapper.query();
        return voList;
    }

    @Override
    @Transactional
    public boolean addCus(CustomerInfoSaveBO saveBO) {
        //1.增加客户信息
        CustomerInfoDTO dto = new CustomerInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        boolean flag = this.insert(dto);
        //2新增用户登录信息
        LoginInfoDTO loginInfoDTO = new LoginInfoDTO();
        //2.0客户id
        loginInfoDTO.setUserId(id);
        //2.1生成主键id
        loginInfoDTO.setId(ymlConfig.getPkId());
        //2.2账号
        loginInfoDTO.setAccount(saveBO.getPhone());
        //2.3密码盐
        String salt = Md5Utils.createSalt();
        loginInfoDTO.setSalt(salt);
        //2.4加密 密码
        //后台管理增加用户设置密码(默认密码为12345678z)
        if (saveBO.getPassword() == null) {
            loginInfoDTO.setPassword(Md5Utils.MD5Encode("12345678z", salt));
        } else {
            loginInfoDTO.setPassword(Md5Utils.MD5Encode(saveBO.getPassword(), salt));
        }
        loginInfoService.insert(loginInfoDTO);
        return flag;
    }
}
